package Main;

import java.util.Vector;
import java.io.IOException;
import Database.BudgetDatabase;
import Database.Database;
import Database.ExcelWriter;

public class ManageItem {
	//@author A0084556W
	protected Vector <Item> itemList = new Vector<Item> ();
	protected Vector <Item> tempMatchingList = new Vector<Item>();
	protected ManageCategory e_cat=new ManageCategory();
	
	protected double[][][] spending = new double [15][13][32];// year month day    
	
	protected double [][] budget = new double [15][13];	
	
	Database db = new Database();  //
	BudgetDatabase bdb = new BudgetDatabase(); //
	ExcelWriter ew = new ExcelWriter();

	public ManageItem() throws IOException{	
		itemList = db.readFile();
		budget = bdb.readArrayFromFile();
	}
	
	public Vector<Item> getList(){
		return itemList;
	}

	public void setBudget(double allowance, int month, int year){
		budget[year - 2000][month] = allowance;
	}
	
	public int getNoOfItem(){
		return itemList.size();
	}
	
	public Item addToItemVector(String name, double price, int necessityValue, int day, int month , int year , String place, String category, String pay){
		Item i= new Item(name, price, necessityValue, day, month, year, place, category, pay);
		itemList.add(i);
		spending[year-2000][month][day] += price;
		
		return i;
	}
	
	public void addToItemVectorUndo(String name, double price, int necessityValue, int day, int month, int year, String place, String category, String pay, int ind){
		itemList.add(ind, new Item(name, price, necessityValue, day, month, year, place, category, pay));
		spending[year-2000][month][day] += price;
	}
	
	//@author A0083934X
	public int deleteFromItemVector(Item tempItem){
		int indexOfItem=-1;
		for (int i = 0; i < itemList.size(); i++){
			if (tempItem.getName().equals(itemList.get(i).getName())){
				if (tempItem.getPrice() == itemList.get(i).getPrice()){
					if (tempItem.getNecessityValue() == itemList.get(i).getNecessityValue()){
						if (tempItem.getDate().getDay() == itemList.get(i).getDate().getDay()){
							if (tempItem.getDate().getMonth() == itemList.get(i).getDate().getMonth()){
								if (tempItem.getDate().getYear() == itemList.get(i).getDate().getYear()){
									if (tempItem.getPlace().equals(itemList.get(i).getPlace())){
										if (tempItem.getCategory().equals(itemList.get(i).getCategory())){	
											if (tempItem.getPay().equals(itemList.get(i).getPay())){
												indexOfItem = i;
												break;
											}
										}
									}
								}
							}
						}
					}
				}
			}
			
		}
		if(indexOfItem == -1){
			return -1;
		}
		else{
			itemList.remove(indexOfItem);
			spending[tempItem.getDate().getYear()-2000][tempItem.getDate().getMonth()][tempItem.getDate().getDay()] -= tempItem.getPrice();			
			return indexOfItem;
		}
	}
	
	public int updateItemVector(Item oldItem, Item updatedItem){
		int indexOfItem=-1;
		for (int i = 0; i < itemList.size(); i++){
			if (oldItem.getName().equals(itemList.get(i).getName())){
				if (oldItem.getPrice() == itemList.get(i).getPrice()){
					if (oldItem.getNecessityValue() == itemList.get(i).getNecessityValue()){
						if (oldItem.getDate().getDay() == itemList.get(i).getDate().getDay()){
							if (oldItem.getDate().getMonth() == itemList.get(i).getDate().getMonth()){
								if (oldItem.getDate().getYear() == itemList.get(i).getDate().getYear()){
									if (oldItem.getPlace().equals(itemList.get(i).getPlace())){
										if (oldItem.getCategory().equals(itemList.get(i).getCategory())){	
											if (oldItem.getPay().equals(itemList.get(i).getPay())){	
												indexOfItem = i;
												break;
											}
										}
									}
								}
							}
						}
					}
				}
			}
			
		}
		if(indexOfItem == -1){
			return -1;
		}
		else{
			
			itemList.setElementAt(updatedItem, indexOfItem);
			spending[oldItem.getDate().getYear()-2000][oldItem.getDate().getMonth()][oldItem.getDate().getDay()] -= oldItem.getPrice();
			spending[updatedItem.getDate().getYear()-2000][updatedItem.getDate().getMonth()][updatedItem.getDate().getDay()] += updatedItem.getPrice();
			return 1;
		}
	}
	
	public Vector<Item> displayItemVector(){
		return itemList;
	}
	
	public Search managerSearch(Item inputItem){
		Search searchItem = new Search();
		searchItem.setItem(inputItem);
		searchItem.setTempItemList(itemList);
		searchItem.doingTheSearchAlgo();
		return searchItem;
	}
	
	
	public Vector<Item> simpleSearch(String inputname){		
		Vector <Item> matchingItems = new Vector <Item>();
		int numberOfItems = itemList.size();
		
		for (int i = 0; i < numberOfItems; i++){
			Item tempItem = itemList.get(i);
			if(inputname.equals(tempItem.getName()))
			
			matchingItems.add(tempItem);
		}
		
		return matchingItems;
		
	}
	
	public Expense getReportAndAlert(int month, int year){
		Expense newExpense = new Expense();
		double sumSpending = 0;
		for (int i = 1; i < 32; i++){
			sumSpending += spending[year-2000][month][i];
		}
		newExpense.setSpending(sumSpending);
		newExpense.setBudget(budget[year-2000][month]);
		
		return newExpense;
	}
	
	public Expense getMonthlyExpense(int m, int y){
		Expense newExpense = new Expense();
		double sumSpending = 0;
		for (int i = 1; i < 32; i++){
			sumSpending += spending[y-2000][m][i];
		}
		newExpense.setSpending(sumSpending);
		return newExpense;
	}
	
	public Expense getDailyExpense(int d, int m, int y){
		Expense newExpense = new Expense();
		newExpense.setSpending(spending[y-2000][m][d]);
		
		return newExpense;
	}
	
	//@author A0084556W
	public void saveData(){
		try {
			db.saveData(itemList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void saveToExcel(boolean isNameWanted,boolean isPriceWanted,boolean isNecessityValueWanted,boolean isDayWanted, 
			boolean isMonthWanted, boolean isYearWanted,boolean isPlaceWanted, boolean isCategoryWanted, boolean isPaymentWanted){
		try{
			ew.saveDataToExcel( itemList, isNameWanted, isPriceWanted, isNecessityValueWanted, isDayWanted, 
					isMonthWanted, isYearWanted, isPlaceWanted, isCategoryWanted, isPaymentWanted);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

	public void saveBudgetData(){
		try{
			bdb.writeArrayToFile(budget);
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

